layui.define(function (exports) {

    var a = layui.jquery,
        admin_map = a("#admin_map").val(),
        tc = '#GalleryTemplate',
        tcb = '#Gallery_',
        k = 'openMaterialDraggable',
        l = a('.' + k),
        layer = layui.layer,
        laytpl = layui.laytpl,
        T = {
            init: function () {
                a(".openGallery").click(function () {
                    var c = a(this).attr('data-c'), n = a(this).attr('data-n'), t = a(this).attr('data-t'),
                        v = a("#Gallery_" + n).find('#checkGallery_' + n).val(), h = 650,
                        w = window.frames.frameElement.id.includes('layui-layer-iframe'), l = w ? parent.layer : layer;
                    if (a(window).height() < h) h = (a(window).height() - 20);
                    v = typeof (v) !== 'undefined' ? v : '';
                    l.open({
                        type: 2,
                        area: ['1000px', h + 'px'],
                        fix: false,
                        maxmin: true,
                        shadeClose: true,
                        shade: 0.4,
                        anim: 2,
                        title: t == '1' ? '选择图片' : (t == '2' ? "选择视频" : '选择音频'),
                        content: '/' + admin_map + '/gallery/open.html?t=' + t + '&c=' + c + "&n=" + n + '&v=' + v,
                    });

                });
                l.on('mouseenter', '.del-wrap', function () {
                    a(this).find('.drop-down').show();
                }).on('mouseleave', '.del-wrap', function () {
                    a(this).find('.drop-down').hide();
                }).on('click', '.drop-del', function () {
                    var v = a(this).attr('data-val'), p = a(this).parents('.' + k).find('.drop-value');
                    var val = p.val().split(',');
                    var index = val.indexOf(v);
                    index > -1 ? val.splice(index, 1) : '';
                    p.val(val.join(','))
                    a(this).parents('.material-preview').remove();
                });

                renderGallery = function (i, u, n, ty) {
                    var t = a(tc + ty).html(), w = window.frames.frameElement.id.includes('layui-layer-iframe'),
                        l = w ? parent.layer : layer;
                    laytpl(t).render({data: u, name: n}, function (html) {
                        a(tcb + n).html(html);
                    });
                    l.close(l.index)
                }
            }
        }


    exports('gallery', T);
});